#include "textures_manager.h"

//
// Forward declaration
//

TexturesManager* TexturesManager::this_pointer;



TexturesManager::TexturesManager(void)
{
  this_pointer = this;
}


TexturesManager::~TexturesManager(void)
{
}


void TexturesManager::Add(int id, LPWSTR texture_name, D3DXCOLOR transcolor) {
  D3DXIMAGE_INFO  image_info;
  LPDIRECT3DTEXTURE9 temp_texture;
  
  HRESULT result = D3DXGetImageInfoFromFile(texture_name, &image_info);
  if(result != D3D_OK) {
     Log::Write(L"Get image info from file fail: ", texture_name);
     return;
  }
  int width = image_info.Width;
  int height = image_info.Height;
  result = D3DXCreateTextureFromFileEx(
    Screen::screen->getDevive(),
    texture_name,
    image_info.Width,
    image_info.Height,
    1,
    D3DPOOL_DEFAULT,
    D3DFMT_UNKNOWN,
    D3DPOOL_DEFAULT,
    D3DX_DEFAULT,
    D3DX_DEFAULT,
    transcolor,
    &image_info,
    NULL,
    &temp_texture);
  if(result != D3D_OK) {
     Log::Write(L"Create texture fail: ", texture_name);
     return;
  }

  //
  // Initialize texture node
  //

  TextureNode *node;
  node = new TextureNode;
  node->id = id;
  node->width = width;
  node->height = height;
  node->pNext = NULL;
  node->texture = temp_texture;

  // Add texture to list
  this->Add(node);
  
}


void TexturesManager::AddArray() {

}


TexturesManager::TextureNode* TexturesManager::GetTextureById(int id) {
  TextureNode *temp = this->texture_head;
  while(temp == NULL) {
    if(temp->id == id) {
      return temp;
    }
    temp = temp->pNext;
  }
}


void TexturesManager::RemoveAll() {

}

void TexturesManager::Add(TextureNode *node) {
  if(this->texture_head == NULL) {
    this->texture_head = node;
  } else {
    node->pNext = texture_head;
    texture_head = node;
  }
}
